<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // // 工厂函数
        // function Person(name,age){
        //     let obj ={};
        //     obj.name = name;
        //     obj.age = age;
        //     return obj
        // }

        // let a = Person('aa',18)
        // console.log(a);
        // console.log(a.constructor);

        // //构造函数
        // function People(name,age){
        //     this.name = name;
        //     this.age = age;
        // }

        let b = new People('bb',3)
        console.log(b);
        // console.log(b.constructor);

        function People(name,age){
            this.name = name;
            this.age = age;
        }

        //封装new
        function myNew(){
            //创建一个空对象
            let obj = {};
            //[]等同于Array.prototype 
            let constructor = [].shift.call(arguments);
            //连接原型
            obj.__proto__ = constructor.prototype;
            //绑定this
            let res = constructor.apply(obj,arguments);
            //判断结果
            return typeof res === 'object' ? res : obj;
        }

        let c = myNew(People,'cc',18)
        console.log(c);
    </script>
</body>
</html>